package com.jh3502.helper.function.listrule;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jh3502.helper.R;
import com.jh3502.helper.base.BaseActivity;
import com.jh3502.helper.utils.SpHelper;
import com.jh3502.helper.utils.Utils;
import com.jh3502.helper.view.MyRecyclerView;

import java.util.ArrayList;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/6/8 0008.
 */

public class ListRuleChooseActivity extends BaseActivity {

    @Nullable
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @Nullable
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @Nullable
    @BindView(R.id.list_rule_choose_recycler)
    MyRecyclerView listRuleChooseRecycler;
    @Nullable
    @BindView(R.id.list_rule_choose_above)
    Button listRuleChooseAbove;
    @Nullable
    @BindView(R.id.list_rule_choose_next)
    Button listRuleChooseNext;
    @NonNull
    private ArrayList<Map<String, Object>> maleList = new ArrayList<>(2);
    @NonNull
    private ArrayList<Map<String, Object>> femaleList = new ArrayList<>(2);
    @NonNull
    private ArrayList<Map<String, Object>> msg = new ArrayList<>(2);
    private ListRuleChooseAdapter mAdapter;
    private int page = 0;
    private String maleID, femaleID;

    @Override
    public void responseError(int code, String msg) {

    }

    @Override
    protected int setViewId() {
        return R.layout.activity_list_rule;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            default:
                break;
        }
        return true;
    }

    @Override
    protected void initView() {
        toolbar.setTitle("");
        toolbarTitle.setText("绑定量体单");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        if (page == 0) {
            listRuleChooseAbove.setVisibility(View.GONE);
        }
        mAdapter = new ListRuleChooseAdapter(this, msg);
        listRuleChooseRecycler.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        maleList = Utils.getInstance().jsa2list(SpHelper.getInstance().getListltruleMale());
        femaleList = Utils.getInstance().jsa2list(SpHelper.getInstance().getListltruleFemale());
        msg.addAll(maleList);
        mAdapter.notifyDataSetChanged();
        mAdapter.setOnItemClickListener(s -> {
            if (page == 0) {
                page++;
                maleID = (String) maleList.get(Integer.parseInt(s)).get("id");
                msg.clear();
                msg.addAll(femaleList);
                mAdapter.notifyDataSetChanged();
                listRuleChooseAbove.setVisibility(View.VISIBLE);
            } else if (page == 1) {
                femaleID = (String) femaleList.get(Integer.parseInt(s)).get("id");
                Intent intent = new Intent();
                intent.putExtra("maleID", maleID);
                intent.putExtra("femaleID", femaleID);
                setResult(Activity.RESULT_OK, intent);
                onBackPressed();
            }
        });
    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @OnClick(R.id.list_rule_choose_above)
    public void onListRuleChooseAboveClicked() {
        page = 0;
        msg.clear();
        msg.addAll(maleList);
        mAdapter.notifyDataSetChanged();
        listRuleChooseAbove.setVisibility(View.GONE);
    }

    @OnClick(R.id.list_rule_choose_next)
    public void onListRuleChooseNextClicked() {
        if (page == 0) {
            listRuleChooseNext.setText("下一步");
            page++;
            listRuleChooseAbove.setVisibility(View.VISIBLE);
            msg.clear();
            msg.addAll(femaleList);
            mAdapter.notifyDataSetChanged();
        } else if (page == 1) {
            listRuleChooseNext.setText("完成");
            Intent intent = new Intent();
            intent.putExtra("maleID", maleID);
            intent.putExtra("femaleID", femaleID);
            setResult(Activity.RESULT_OK, intent);
            onBackPressed();
        }
    }
}
